//
//  NewsImageCell.m
//  suRead
//
//  Created by lidi on 15/11/8.
//  Copyright © 2015年 bao. All rights reserved.
//

#import "UIImageView+WebCache.h"
#import "NewsImageCell.h"
#import "M_News.h"
#import "AccountManager.h"
#import "NewCellManager.h"
#import "Masonry.h"

@interface NewsImageCell()
@property (weak, nonatomic) IBOutlet UILabel *labelTitle;
@property (weak, nonatomic) IBOutlet UILabel *labelCreateTime;

@property (weak, nonatomic) IBOutlet UIImageView *imageView_1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView_2;
@property (weak, nonatomic) IBOutlet UIImageView *imageView_3;
@property (weak, nonatomic) IBOutlet UIView *viewCountContainer;
@property (weak, nonatomic) IBOutlet UIImageView *imageViewFlag;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *flagWidthConstraint;

@end

@implementation NewsImageCell

//- (void)setFrame:(CGRect)frame{
//    frame.origin.y += 5;
//    frame.size.height -= 5;
//    [super setFrame:frame];
//}

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
    if ( [[[UIDevice currentDevice]systemVersion] floatValue] >= 8.0) {
        self.layoutMargins = UIEdgeInsetsZero;
        self.preservesSuperviewLayoutMargins = NO;
        self.separatorInset = UIEdgeInsetsMake(0, 15.0f, 0, 15.0f);
    }
    
    UIView *bottomLine = [UIView new];
    bottomLine.backgroundColor = [UIColor colorWithRed:0xee / 255.0f green:0xee / 255.0f blue:0xee / 255.0f alpha:1];
    [self.contentView addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bottomLine.superview).offset(15.0);
        make.right.equalTo(bottomLine.superview).offset(-15.0);
        make.bottom.equalTo(bottomLine.superview);
        make.height.equalTo(@.5f);
    }];
}

- (void)setNewsData:(M_News *)newsData
{
    NSArray *images = newsData.f_images;
    
    if ([images count] > 0) {
        NSString *imageURL = [newsData.f_images objectAtIndex:0];
        [self.imageView_1 sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:nil];
    }
    if ([images count] > 1) {
        NSString *imageURL = [newsData.f_images objectAtIndex:1];
        [self.imageView_2 sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:nil];
    }
    if ([images count] > 2) {
        NSString *imageURL = [newsData.f_images objectAtIndex:2];
        [self.imageView_3 sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:nil];
    }
    
    NSString *title = newsData.f_title;
    self.labelTitle.text = title;
    
    //已读未读
    if ([[NewCellManager shared] hasRead:newsData.f_id]) {
        self.labelTitle.textColor = [UIColor lightGrayColor];
    } else {
        self.labelTitle.textColor = [UIColor blackColor];
    }
    
    NSString *createTime = newsData.f_updateTime;
    self.labelCreateTime.text = createTime;

    /*
     置顶|99
     最新|1
     实发|2
     热点|3
     独家|4
     专题|5
     推广|6
     视频|7
     直播|8
     福利9
     */
    self.flagWidthConstraint.constant = 36.0f;
    NSString *flagName = @"";
    switch ([newsData.f_newsFlag intValue]) {
        case 99:
            flagName = @"news_flag_01";
            break;
        case 1:
            flagName = @"news_flag_02";
            break;
        case 2:
            flagName = @"news_flag_03";
            break;
        case 3:
            flagName = @"news_flag_04";
            break;
        case 4:
            flagName = @"news_flag_05";
            break;
        case 5:
            flagName = @"news_flag_06";
            break;
        case 6:
            flagName = @"news_flag_07";
            break;
        case 7:
            flagName = @"news_flag_08";
            break;
        case 8:
            flagName = @"news_flag_09";
            break;
        case 9:
            flagName = @"news_flag_10";
            break;
        default:
            flagName = @"icon_time";
            self.flagWidthConstraint.constant = 15.0f;
            break;
    }
    self.imageViewFlag.image = [UIImage imageNamed:flagName];
    
    if ([AccountManager sharedAccountManager].f_isShowViewCount) {
        self.viewCountContainer.hidden = NO;
        NSString *viewCount = [NSString stringWithFormat:@"%@",newsData.f_views];
        self.labelViewCount.text = viewCount;
        self.lableZan.text = [NSString stringWithFormat:@"%ld",newsData.f_square];
    } else {
        self.viewCountContainer.hidden = YES;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
